import org.w3c.dom.Node;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

//N 叉树的层序遍历
//测试链接 https://leetcode.cn/problems/n-ary-tree-level-order-traversal/
public class LevelOrder {

    class Node {
        public int val;
        public List<Node> children;

        public Node() {}

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val, List<Node> _children) {
            val = _val;
            children = _children;
        }
    };

    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> ret = new ArrayList<>();
        Queue<Node> queue = new LinkedList();;
        if(root == null) return ret;
        queue.add(root);

        while(!queue.isEmpty()) {
            int n = queue.size();
            List<Integer> temp = new ArrayList<>();

            for(int i = 0; i < n; i++) {
                Node node =  queue.poll();
                temp.add(node.val);
                for(Node c : node.children) {
                    queue.add(c);
                }
            }
            ret.add(temp);
        }
        return ret;
    }
}
